package com.gxa.ssm251.controller;


import com.gxa.ssm251.pojo.*;
import com.gxa.ssm251.service.*;
import com.gxa.ssm251.util.JsonResult;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;

import java.sql.Timestamp;

import java.util.List;
import java.util.Map;

/**
 * 内容管理模块
 *
 * @author 舒空
 */
//@WebAppConfiguration
@CrossOrigin//解决跨域
@RestController
@RequestMapping("/comment")
@Api(value = "司机评价", produces = "application/json:charset=UTF-8",
        tags = "司机评价")
public class CommentController {
    @Resource
    private CommentService commentService;

    @ApiOperation(value = "查询全部评价 page,size")
    @PostMapping("/selectAllByPage")
    public JsonResult selectAllComment(@RequestBody Map<String,Object> map) {
        JsonResult jsonResult = new JsonResult();
        Integer page = Integer.valueOf(map.get("page").toString());
        Integer size = Integer.valueOf(map.get("size").toString());
        List<Comment> commentList = commentService.selectAllByPage(page, size);
        Integer comments = commentService.selectAllComment();
        if (commentList != null) {
            jsonResult.setData(commentList);
            jsonResult.setCount(comments);
            jsonResult.setMsg("查询成功");
            jsonResult.setCode("1");
        } else {
            jsonResult.setMsg("查询失败");
            jsonResult.setCode("0");
        }
        return jsonResult;
    }

    @ApiOperation(value = "多条件模糊查询")
    @PostMapping("/selectLikeParam")
    public JsonResult selectLikeParam(@RequestBody Map<String,Object> map) {
        String param = map.get("param").toString();
        Timestamp startTime = Timestamp.valueOf(map.get("startTime").toString());
        Timestamp endTime = Timestamp.valueOf(map.get("endTime").toString());
        List<Comment> comments = commentService.selectLikeParam(param,startTime,endTime);
        JsonResult jsonResult = new JsonResult();
        if (comments.size() != 0) {
            jsonResult.setData(comments);
            jsonResult.setMsg("查询成功");
            jsonResult.setCode("1");
        } else {
            jsonResult.setMsg("查询失败");
            jsonResult.setCode("0");
        }
        return jsonResult;
    }

    @ApiOperation(value = "用订单编号修改")
    @PostMapping("/updateIsVisible")
    public JsonResult updateIsVisible(@RequestBody Comment comment) {
        Integer comments = commentService.updateIsVisible(comment.getIsVisible(),comment.getOrderNumber());
        JsonResult jsonResult = new JsonResult();
        if (comments>0) {
            jsonResult.setData(comments);
            jsonResult.setMsg("修改成功");
            jsonResult.setCode("1");
        } else {
            jsonResult.setMsg("修改失败");
            jsonResult.setCode("0");
        }
        return jsonResult;
    }

    @ApiOperation(value = "用订单编号删除")
    @PostMapping("/deleteByOrderNumber")
    public JsonResult deleteByOrderNumber(@RequestBody Comment comment) {

        Integer integer = commentService.delete(comment.getOrderNumber());
        JsonResult jsonResult = new JsonResult();
        if (integer > 0) {
            jsonResult.setCode("1");
            jsonResult.setMsg("删除成功");
        } else {
            jsonResult.setMsg("删除失败");
            jsonResult.setCode("0");
        }
        return jsonResult;
    }

    /*@ApiOperation(value = "时间查询")
    @PostMapping("/selectByTime")
    public JsonResult selectByTime(@RequestBody Map<String, String> map) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Comment> comments = commentService.selectByTime(dateFormat.parse(map.get("date1")), dateFormat.parse(map.get("date2")));
        //List<Comment> comments = commentService.selectByTime((Date) map.get("date1"), (Date) map.get("date2"));

        JsonResult jsonResult = new JsonResult();
        if (comments.size() != 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(comments);
        } else {
            jsonResult.setMsg("查询失败");
            jsonResult.setCode("400");
        }
        return jsonResult;
    }*/
}
